<template>
  <div class="app-container">
    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px" style="width: 80%">
      <el-form-item label="第三方仓库" prop="field101">
        <el-select v-model="formData.warehouseId" placeholder="请选择第三方仓库" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in warehouses" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入库商品" prop="goods">
        <el-select v-model="formData.goodsId" placeholder="请选择入库商品" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in goodList" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入库数量" prop="field104">
        <el-input v-model="formData.supplierStock" placeholder="请输入入库数量" clearable :style="{width: '100%'}">
        </el-input>
      </el-form-item>
      <el-form-item size="large">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {list3rdwarehouse} from "@/api/system/3rdwarehouse";
import {listGoodsManage, listSelect} from "@/api/supplier/goodsManage";
import user from "@/store/modules/user";
import {isNumberStr} from "@/utils";
import {update3rdstockByAdd} from "@/api/supplier/3rdstock";

export default {
  components: {},
  props: [],
  data() {
    return {
      formData: {
        warehouseId: undefined,
        updateBy: user.state.id,
        supplierStock: undefined,
        goodsId: undefined,
      },
      rules: {},
      warehouses: [],
      goodList: [],
    }
  },
  computed: {},
  watch: {},
  created() {
    this.get3rdstoreList();
    this.getGoodList();
  },
  mounted() {},
  methods: {
    get3rdstoreList() {
      list3rdwarehouse().then(response => {
        response.rows.forEach((item) => {
          this.warehouses.push({
            "label": item.warehouseName,
            "value": item.warehouseId,
          })
        })
      });
    },
    getGoodList() {
      listSelect({
        supplierId: user.state.id
      }).then(response => {
        console.log(response);
        response.data.forEach((item) => {
          this.goodList.push({
            "label": item.goodsName,
            "value": item.goodsId
          })
        })
      });
    },
    submitForm() {
      if (this.formData.updateBy == null) {
        this.$notify.error({
          title: '错误',
          message: '请先登录'
        });
        return;
      }
      if (this.formData.warehouseId == null) {
        this.$notify.error({
          title: '错误',
          message: '请选择第三方仓库'
        });
        return;
      }
      if (this.formData.supplierStock == null || !isNumberStr(this.formData.supplierStock)) {
        this.$notify.error({
          title: '错误',
          message: '请输入数字'
        });
        return;
      }
      if (parseInt(this.formData.supplierStock).toString() !== this.formData.supplierStock ||
        parseInt(this.formData.supplierStock) <= 0 || parseInt(this.formData.supplierStock) >= 2000000) {
        this.$notify.error({
          title: '错误',
          message: '请输入大于0小于200000的整数'
        });
        return;
      }
      if (this.formData.goodsId == null) {
        this.$notify.error({
          title: '错误',
          message: '请选择商品'
        });
        return;
      }
      this.$notify({
        title: '成功',
        message: '入库申请提交成功',
        type: 'success'
      });

      update3rdstockByAdd(this.formData).then(response => {
        console.log(response);
        if (response.code === 200) {
          this.$modal.msgSuccess("入库成功");
          this.resetForm();
        } else {
          this.$modal.msgError("入库失败");
          this.resetForm();
        }
      });
    },
    resetForm() {
      this.formData.supplierStock = '';
      this.formData.warehouseId = '';
      this.formData.goodId = '';
    },
  }
}

</script>
<style>
</style>
